home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / forms / FORMS / slider.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  6KB  |  222 lines

  1. /*
  2.  * slider.c
  3.  *
  4.  * Forms Object class: SLIDER, VALSLIDER
  5.  *
  6.  * Written by: Mark Overmars
  7.  *
  8.  * Version 2.1 b
  9.  * Date: Sep 29, 1992
  10.  */
  11.  
  12. #include <malloc.h>
  13. #include <sys/types.h>
  14. #include <stdio.h>
  15. #include "forms.h"
  16.  
  17. /* The special information for sliders. */
  18. typedef struct {
  19.    float min;        /* minimal value of slider */
  20.    float max;        /* maximal value of slider */
  21.    float val;        /* current value of slider */
  22.    float step;        /* step size */
  23.    float slsize;    /* size of the slider */
  24.    int prec;            /* precision when printing value */
  25.    int always;        /* whether always returning value */
  26. } SPEC;
  27.  
  28. static void draw_slider(FL_OBJECT *ob)
  29. /* Draws a slider */
  30. {
  31.   SPEC *sp = (SPEC *) ob->spec;
  32.   char valstr[32];
  33.   float val;
  34.   float sxx = ob->x, syy = ob->y, sww = ob->w, shh = ob->h;
  35.   float bxx = ob->x, byy = ob->y, bww = ob->w, bhh = ob->h;
  36.  
  37.   /* Draw the value box */
  38.   if (ob->objclass == FL_VALSLIDER)
  39.   {
  40.     if (ob->type == FL_VERT_SLIDER || ob->type == FL_VERT_FILL_SLIDER ||
  41.         ob->type == FL_VERT_NICE_SLIDER)
  42.       { byy = syy + shh - 25.0; bhh = 25.0; shh -= 25.0; }
  43.     else if (ob->type == FL_HOR_SLIDER || ob->type == FL_HOR_FILL_SLIDER ||
  44.         ob->type == FL_HOR_NICE_SLIDER)
  45.       { bww = 50.0; sxx += 50.0; sww -= 50.0; }
  46.     sprintf(valstr,"%.*f",sp->prec,sp->val);
  47.     fl_drw_box(ob->boxtype,bxx,byy,bww,bhh,ob->col1,FL_SLIDER_BW1);
  48.     fl_drw_text_beside(FL_ALIGN_CENTER,bxx,byy,bww,bhh,
  49.                           ob->lcol,ob->lsize,ob->lstyle,valstr);
  50.   }
  51.   /* Draw the slider */
  52.   if (sp->min == sp->max)
  53.     val = 0.5;
  54.   else
  55.     val = (sp->val - sp->min)/(sp->max - sp->min);
  56.   if (ob->align == FL_ALIGN_CENTER)
  57.     fl_drw_slider( ob->boxtype,sxx,syy,sww,shh,ob->col1,ob->col2,
  58.             ob->type,sp->slsize,val,ob->label);
  59.   else
  60.   {
  61.     fl_drw_slider( ob->boxtype,sxx,syy,sww,shh,ob->col1,ob->col2,
  62.             ob->type,sp->slsize,val,"");
  63.     fl_drw_text_beside(ob->align,ob->x,ob->y,ob->w,ob->h,
  64.             ob->lcol,ob->lsize,ob->lstyle,ob->label);
  65.   }
  66. }
  67.  
  68. static int handle_mouse(FL_OBJECT *ob,float mx, float my)
  69. /* Handle a mouse position change */
  70. {
  71.   SPEC *sp = (SPEC *) ob->spec;
  72.   float oldval,newval;
  73.   float sxx = ob->x, syy = ob->y, sww = ob->w, shh = ob->h;
  74.   /* Change the slider size */
  75.   if (ob->objclass == FL_VALSLIDER)
  76.   {
  77.     if (ob->type == FL_VERT_SLIDER || ob->type == FL_VERT_FILL_SLIDER ||
  78.         ob->type == FL_VERT_NICE_SLIDER)
  79.       { shh -= 25.0; }
  80.     else if (ob->type == FL_HOR_SLIDER || ob->type == FL_HOR_FILL_SLIDER ||
  81.         ob->type == FL_HOR_NICE_SLIDER)
  82.       { sxx += 50.0; sww -= 50.0; }
  83.   }
  84.   /* Calculate the value */
  85.   if (sp->min == sp->max)
  86.     oldval = 0.5;
  87.   else
  88.     oldval = (sp->val - sp->min)/(sp->max - sp->min);
  89.   fl_get_pos_in_slider(sxx,syy,sww,shh,ob->type,sp->slsize,
  90.             mx,my,oldval,&newval);
  91.   newval = sp->min + newval*(sp->max - sp->min);
  92.   if (sp->step != 0.0) newval = (int) (newval/sp->step +0.5) * sp->step;
  93.   if (newval < sp->min) newval = sp->min;
  94.   if (newval > sp->max) newval = sp->max;
  95.   if (sp->val != newval) 
  96.     { sp->val = newval; fl_redraw_object(ob); return 1; }
  97.   else
  98.      return 0;
  99. }
  100.  
  101. static int handle_slider(FL_OBJECT *ob,int event,float mx,float my,char key)
  102. /* Handles an event */
  103. {
  104.   SPEC *sp = (SPEC *) ob->spec;
  105.   switch (event)
  106.   {
  107.     case FL_DRAW:
  108.     draw_slider(ob);
  109.     return 0;
  110.     case FL_ENTER:
  111.     case FL_LEAVE:
  112.     return 0;
  113.     case FL_PUSH:
  114.     case FL_MOUSE:
  115.         return ( handle_mouse(ob,mx,my)  && sp->always);
  116.     case FL_RELEASE:
  117.         return (! sp->always);
  118.     case FL_FREEMEM:
  119.     free(ob->spec);
  120.     return 0;
  121.   }
  122.   return 0;
  123. }
  124.  
  125. /*------------------------------*/
  126.  
  127. static FL_OBJECT *create_it(int objclass, int type, float x, float y,
  128.                 float w, float h, char label[])
  129. /* creates an object */
  130. {
  131.   FL_OBJECT *ob;
  132.   ob = fl_make_object(objclass,type,x,y,w,h,label,handle_slider);
  133.   ob->boxtype = FL_SLIDER_BOXTYPE;
  134.   ob->col1 = FL_SLIDER_COL1;
  135.   ob->col2 = FL_SLIDER_COL2;
  136.   ob->align = FL_SLIDER_ALIGN;
  137.   ob->lcol = FL_SLIDER_LCOL;
  138.  
  139.   ob->spec = (int *) fl_malloc(sizeof(SPEC));
  140.   ((SPEC *)(ob->spec))->min = 0.0;
  141.   ((SPEC *)(ob->spec))->max = 1.0;
  142.   ((SPEC *)(ob->spec))->val = 0.5;
  143.   ((SPEC *)(ob->spec))->step = 0.0;
  144.   ((SPEC *)(ob->spec))->slsize = FL_SLIDER_WIDTH;
  145.   ((SPEC *)(ob->spec))->prec = 2;
  146.   ((SPEC *)(ob->spec))->always = TRUE;
  147.  
  148.   return ob;
  149. }
  150.  
  151. static FL_OBJECT *add_it(int objclass, int type, float x, float y,
  152.                  float w, float h, char label[])
  153. /* Adds an object */
  154. {
  155.   FL_OBJECT *ob;
  156.   ob = create_it(objclass,type,x,y,w,h,label);
  157.   fl_add_object(fl_current_form,ob);
  158.   return ob;
  159. }
  160.  
  161. FL_OBJECT *fl_create_slider(int type, float x, float y, float w, float h,
  162.                 char label[])
  163.   { return create_it(FL_SLIDER,type,x,y,w,h,label); }
  164.  
  165. FL_OBJECT *fl_add_slider(int type, float x, float y, float w, float h,
  166.                  char label[])
  167.   { return add_it(FL_SLIDER,type,x,y,w,h,label); }
  168.  
  169. FL_OBJECT *fl_create_valslider(int type, float x, float y, float w, float h,
  170.                 char label[])
  171.   { return create_it(FL_VALSLIDER,type,x,y,w,h,label); }
  172.  
  173. FL_OBJECT *fl_add_valslider(int type, float x, float y, float w, float h,
  174.                  char label[])
  175.   { return add_it(FL_VALSLIDER,type,x,y,w,h,label); }
  176.  
  177. /*-------------------------------------------*/
  178.  
  179. void fl_set_slider_value(FL_OBJECT *ob,float val)
  180. {
  181.   ((SPEC *)(ob->spec))->val = val;
  182.   fl_redraw_object(ob);
  183. }
  184. void fl_set_slider_bounds(FL_OBJECT *ob,float min,float max)
  185. {
  186.   ((SPEC *)(ob->spec))->min = min;
  187.   ((SPEC *)(ob->spec))->max = max;
  188.   fl_redraw_object(ob);
  189. }
  190.  
  191. float fl_get_slider_value(FL_OBJECT *ob)
  192. /* Returns value of the slider */
  193.   { return ((SPEC *)(ob->spec))->val; }
  194.  
  195. void fl_get_slider_bounds(FL_OBJECT *ob,float *min,float *max)
  196. /* Returns the slider bounds. */
  197. {
  198.   *min = ((SPEC *)(ob->spec))->min;
  199.   *max = ((SPEC *)(ob->spec))->max;
  200. }
  201.  
  202. void fl_set_slider_return(FL_OBJECT *ob, int value)
  203. /* Sets whether to return value all the time */
  204.   { ((SPEC *)(ob->spec))->always = value; }
  205.  
  206. void fl_set_slider_step(FL_OBJECT *ob, float value)
  207. /* Sets the step size to which values are rounded. */
  208.   { ((SPEC *)(ob->spec))->step = value; }
  209.  
  210. void fl_set_slider_size(FL_OBJECT *ob,float size)
  211. /* Sets the portion of the slider box covered by the slider */
  212. {
  213.   if (size <= 0.0) ((SPEC *)(ob->spec))->slsize = 0.001;
  214.   else if (size >= 1.0) ((SPEC *)(ob->spec))->slsize = 1.0;
  215.   else ((SPEC *)(ob->spec))->slsize = size;
  216.   fl_redraw_object(ob);
  217. }
  218.  
  219. void fl_set_slider_precision(FL_OBJECT *ob, int prec)
  220. /* Only for value sliders. */
  221.   { ((SPEC *)(ob->spec))->prec = prec; fl_redraw_object(ob);}
  222.